﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Clandestine
{
    // Shouldn't really be in Conversations namespace, just the main one is too cluttered for now.
    public class ResizableBG
    {
        // Sprites
        public Sprite top;
        public Sprite top_left;
        public Sprite top_right;
        public Sprite bottom;
        public Sprite bottom_left;
        public Sprite bottom_right;
        public Sprite middle;
        public Sprite left;
        public Sprite right;

        private Size size = new Size();
        public Size Size
        {
            get { return size; }
            set { this.Resize(value); }
        }

        public int Width { get { return size.Width; } }
        public int Height { get { return size.Height;  } }

        private Point position = new Point();
        public Point Position
        {
            get { return position; }
            set { this.Reposition(value); }
        }

        public float Layer
        {
            set
            {
                top.Layer = value;
                top_left.Layer = value;
                top_right.Layer = value;
                bottom.Layer = value;
                bottom_right.Layer = value;
                bottom_left.Layer = value;
                left.Layer = value;
                right.Layer = value;
                middle.Layer = value;
            }
            get { return middle.Layer; }
        }

        public bool Visible
        {
            set
            {
                top.Visible = value;
                top_left.Visible = value;
                top_right.Visible = value;
                bottom.Visible = value;
                bottom_right.Visible = value;
                bottom_left.Visible = value;
                left.Visible = value;
                right.Visible = value;
                middle.Visible = value;
            }
            get { return middle.Visible; }
        }

        public virtual bool GluedToCamera
        {
            set
            {
                top.GluedToCamera = value;
                top_left.GluedToCamera = value;
                top_right.GluedToCamera = value;
                bottom.GluedToCamera = value;
                bottom_right.GluedToCamera = value;
                bottom_left.GluedToCamera = value;
                left.GluedToCamera = value;
                right.GluedToCamera = value;
                middle.GluedToCamera = value;
            }
            get { return middle.GluedToCamera; }
        }

        public ResizableBG()
        {
            top = new Sprite(Texture.GetTexture("bg/top.png"));
            top_left = new Sprite(Texture.GetTexture("bg/top_left.png"));
            top_right = new Sprite(Texture.GetTexture("bg/top_right.png"));
            bottom = new Sprite(Texture.GetTexture("bg/bottom.png"));
            bottom_left = new Sprite(Texture.GetTexture("bg/bottom_left.png"));
            bottom_right = new Sprite(Texture.GetTexture("bg/bottom_right.png"));
            middle = new Sprite(Texture.GetTexture("bg/middle.png"));
            left = new Sprite(Texture.GetTexture("bg/left.png"));
            right = new Sprite(Texture.GetTexture("bg/right.png"));
        }
        public ResizableBG(Point position, Size size)
            : this()
        {
            this.Redraw(position, size);
        }

        public void Resize(Size size)
        {
            this.Redraw(this.Position, size);
        }

        public void Reposition(Point position)
        {
            this.Redraw(position, this.Size);
        }

        public void Redraw(Point position, Size size)
        {
            this.size = size;
            this.position = position;

            // Corners!
            top_left.Position = new Point(position.X, position.Y);
            top_right.Position = new Point(position.X + size.Width - top_right.UnscaledWidth, position.Y);
            bottom_left.Position = new Point(position.X, position.Y + size.Height - bottom_left.UnscaledHeight);
            bottom_right.Position = new Point(position.X + size.Width - bottom_right.UnscaledWidth, position.Y + size.Height - bottom_right.UnscaledHeight);

            // Scale everything else!
            top.Position = new Point(position.X + top_left.UnscaledWidth, position.Y);
            top.ScaleX = (((float)(size.Width - top_left.UnscaledWidth - top_right.UnscaledWidth)) / (float)top.UnscaledWidth);
            bottom.Position = new Point(position.X + bottom_left.UnscaledWidth, position.Y + size.Height - bottom.UnscaledHeight);
            bottom.ScaleX = (((float)(size.Width - bottom_left.UnscaledWidth - bottom_right.UnscaledWidth)) / (float)bottom.UnscaledWidth);
            left.Position = new Point(position.X, position.Y + top_left.UnscaledHeight);
            left.ScaleY = (((float)(size.Height - top_left.UnscaledHeight - bottom_left.UnscaledHeight)) / (float)left.UnscaledHeight);
            right.Position = new Point(position.X + size.Width - right.UnscaledWidth, position.Y + top_right.UnscaledHeight);
            right.ScaleY = (((float)(size.Height - top_right.UnscaledHeight - bottom_right.UnscaledHeight)) / (float)right.UnscaledHeight);
            middle.Position = new Point(position.X + top_left.UnscaledWidth, position.Y + top_right.UnscaledWidth);
            middle.ScaleX = (((float)(size.Width - top_left.UnscaledWidth - top_right.UnscaledWidth)) / (float)top.UnscaledWidth);
            middle.ScaleY = (((float)(size.Height - top_left.UnscaledHeight - bottom_left.UnscaledHeight)) / (float)left.UnscaledHeight);
        }

        public virtual void Dispose()
        {
            top.Dispose();
            top_left.Dispose();
            top_right.Dispose();
            bottom.Dispose();
            bottom_right.Dispose();
            bottom_left.Dispose();
            left.Dispose();
            right.Dispose();
            middle.Dispose();
        }
    }
}
